package com.yubb.common.xss;

import com.yubb.common.core.domain.BaseEntity;
import com.yubb.common.core.domain.saas.vo.SysUserVO;
import com.yubb.common.utils.ShiroUtils;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 默认插入实体属性值
 *
 * @author dengjie
 * @version 1.0
 * @date 2023/9/6 22:54
 */
@Aspect
@Component
public class EntityDefaultValuesAspect {

    // 在插入操作前设置默认值
    @Before("execution(* com.ruoyi.production.controller.*.add*(..)) && args(entity)")
    public void beforeSave(BaseEntity entity) {
        Date now = new Date();
        SysUserVO loginUser = ShiroUtils.getSysUser();
        entity.setCreateTime(now);
        entity.setUpdateTime(now);
        // 设置当前用户的ID，可以从Spring Security或其他认证机制中获取
        entity.setCreateBy(loginUser.getId());
        entity.setUpdateBy(loginUser.getId());
    }

    // 在更新操作前设置默认值
    @Before("execution(* com.ruoyi.production.controller.*.edit*(..)) && args(entity)")
    public void beforeUpdate(BaseEntity entity) {
        Date now = new Date();
        SysUserVO loginUser = ShiroUtils.getSysUser();
        entity.setUpdateTime(now);
        // 设置当前用户的ID，可以从Spring Security或其他认证机制中获取
        entity.setUpdateBy(loginUser.getId());
    }
}